﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowArmory

    ''' <summary>
    ''' A class to search characters on the Armory with
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CharacterSearch
        Inherits WowArmoryData

#Region "Declarations"

        Private cssPage As New CharacterSearchSchema.page
        Private strSearchText As String

#End Region

#Region "Properties"

        ''' <summary>
        ''' The search text to search for characters on the Armory with
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property SearchText() As String
            Get
                Return strSearchText
            End Get
            Set(ByVal value As String)
                strSearchText = value
            End Set
        End Property

        ''' <summary>
        ''' The URL that will be used to search for characters on the Armory with
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL() As String
            Get
                Dim ubURL As New URLBuilder("http://www.wowarmory.com/search.xml")

                ubURL.Add("searchType", "characters")
                ubURL.Add("searchQuery", strSearchText)

                Return ubURL.ToString()
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request does not require login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin() As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Returns a list of search characters
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Characters() As List(Of Character)
            Get
                If cssPage Is Nothing Then
                    Return New List(Of Character)
                End If
                If cssPage.armorySearch(0).searchResults(0).characters(0).character Is Nothing Then
                    Return New List(Of Character)
                End If
                Return (From a In cssPage.armorySearch(0).searchResults(0).characters(0).character Select New Character(a.battleGroup, a.battleGroupId, CType(a.classId, ClassType), CType(a.factionId, FactionType), CType(a.genderId, GenderType), a.guild, a.guildId, CDate(a.lastLoginDate), a.level, a.name, CType(a.raceId, RaceType), a.realm, a.relevance, a.searchRank, a.url)).ToList()
            End Get
        End Property

#End Region

#Region "Methods"

        ''' <summary>
        ''' A default constructor
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' A constructor that indicates the name of the person to search for, and performs the search
        ''' </summary>
        ''' <param name="strSearchText"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strSearchText As String)
            Me.strSearchText = strSearchText
            Search()
        End Sub

        ''' <summary>
        ''' Performs the search on the armory
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Search()
            MyBase.Retrieve()

            Dim xsSerializer As New XmlSerializer(cssPage.GetType())

            Dim srXML As New StringReader(MyBase.strData)

            cssPage = CType(xsSerializer.Deserialize(srXML), CharacterSearchSchema.page)
        End Sub

#End Region

#Region "Nested Classes"

        ''' <summary>
        ''' A class that encapsulates a single character returned by the Armory search
        ''' </summary>
        ''' <remarks></remarks>
        Public Class Character

            Private strBattleGroup As String
            Private intBattleGroupID As Integer
            Private cClass As ClassType
            Private fFaction As FactionType
            Private gGender As GenderType
            Private strGuild As String
            Private intGuildID As Integer
            Private dtLastLoginDate As Date
            Private intLevel As Integer
            Private strName As String
            Private rRace As RaceType
            Private strRealm As String
            Private intRelevance As Integer
            Private intSearchRank As Integer
            Private strURL As String

            ''' <summary>
            ''' The battlegroup the character belongs to
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property BattleGroup() As String
                Get
                    Return strBattleGroup
                End Get
                Set(ByVal value As String)
                    strBattleGroup = value
                End Set
            End Property

            ''' <summary>
            ''' The ID number of the battlegroup the character belongs to
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property BattleGroupID() As Integer
                Get
                    Return intBattleGroupID
                End Get
                Set(ByVal value As Integer)
                    intBattleGroupID = value
                End Set
            End Property

            ''' <summary>
            ''' The character's class
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property [Class]() As ClassType
                Get
                    Return cClass
                End Get
                Set(ByVal value As ClassType)
                    cClass = value
                End Set
            End Property

            ''' <summary>
            ''' The character's faction
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Faction() As FactionType
                Get
                    Return fFaction
                End Get
                Set(ByVal value As FactionType)
                    fFaction = value
                End Set
            End Property

            ''' <summary>
            ''' The character's gender
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Gender() As GenderType
                Get
                    Return gGender
                End Get
                Set(ByVal value As GenderType)
                    gGender = value
                End Set
            End Property

            ''' <summary>
            ''' The character's guild
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Guild() As String
                Get
                    Return strGuild
                End Get
                Set(ByVal value As String)
                    strGuild = value
                End Set
            End Property

            ''' <summary>
            ''' The ID number of the character's guild
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property GuildID() As Integer
                Get
                    Return intGuildID
                End Get
                Set(ByVal value As Integer)
                    intGuildID = value
                End Set
            End Property

            ''' <summary>
            ''' The last login date of the character
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property LastLoginDate() As Date
                Get
                    Return dtLastLoginDate
                End Get
                Set(ByVal value As Date)
                    dtLastLoginDate = value
                End Set
            End Property

            ''' <summary>
            ''' The character's level
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Level() As Integer
                Get
                    Return intLevel
                End Get
                Set(ByVal value As Integer)
                    intLevel = value
                End Set
            End Property

            ''' <summary>
            ''' The name of the character
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Name() As String
                Get
                    Return strName
                End Get
                Set(ByVal value As String)
                    strName = value
                End Set
            End Property

            ''' <summary>
            ''' The character's race
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Race() As RaceType
                Get
                    Return rRace
                End Get
                Set(ByVal value As RaceType)
                    rRace = value
                End Set
            End Property

            ''' <summary>
            ''' The realm the character belongs to
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Realm() As String
                Get
                    Return strRealm
                End Get
                Set(ByVal value As String)
                    strRealm = value
                End Set
            End Property

            ''' <summary>
            ''' The relevance of the result
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Relevance() As Integer
                Get
                    Return intRelevance
                End Get
                Set(ByVal value As Integer)
                    intRelevance = value
                End Set
            End Property

            ''' <summary>
            ''' The order in which this character appears in the current search results
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property SearchRank() As Integer
                Get
                    Return intSearchRank
                End Get
                Set(ByVal value As Integer)
                    intSearchRank = value
                End Set
            End Property

            ''' <summary>
            ''' The URL of the character's Armory page
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property URL() As String
                Get
                    Return strURL
                End Get
                Set(ByVal value As String)
                    strURL = value
                End Set
            End Property

            ''' <summary>
            ''' A constructor to create a new character
            ''' </summary>
            ''' <param name="strBattleGroup"></param>
            ''' <param name="intBattleGroupID"></param>
            ''' <param name="cClass"></param>
            ''' <param name="fFaction"></param>
            ''' <param name="gGender"></param>
            ''' <param name="strGuild"></param>
            ''' <param name="intGuildID"></param>
            ''' <param name="dtLastLoginDate"></param>
            ''' <param name="intLevel"></param>
            ''' <param name="strName"></param>
            ''' <param name="rRace"></param>
            ''' <param name="strRealm"></param>
            ''' <param name="intRelevance"></param>
            ''' <param name="intSearchRank"></param>
            ''' <param name="strURL"></param>
            ''' <remarks></remarks>
            Public Sub New(ByVal strBattleGroup As String, ByVal intBattleGroupID As Integer, ByVal cClass As ClassType, ByVal fFaction As FactionType, ByVal gGender As GenderType, ByVal strGuild As String, ByVal intGuildID As Integer, ByVal dtLastLoginDate As Date, ByVal intLevel As Integer, ByVal strName As String, ByVal rRace As RaceType, ByVal strRealm As String, ByVal intRelevance As Integer, ByVal intSearchRank As Integer, ByVal strURL As String)
                Me.strBattleGroup = strBattleGroup
                Me.intBattleGroupID = intBattleGroupID
                Me.cClass = cClass
                Me.fFaction = fFaction
                Me.gGender = gGender
                Me.strGuild = strGuild
                Me.intGuildID = intGuildID
                Me.dtLastLoginDate = dtLastLoginDate
                Me.intLevel = intLevel
                Me.strName = strName
                Me.rRace = rRace
                Me.strRealm = strRealm
                Me.intRelevance = intRelevance
                Me.intSearchRank = intSearchRank
                Me.strURL = strURL
            End Sub

        End Class

#End Region

    End Class

End Namespace